/*
 * NewCheckUpDialog.java
 *
 * Created on May 25, 2008, 4:48 PM
 */

package org.potomak.patientmanager.gui;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.awt.event.WindowEvent;

import javax.jdo.Transaction;
import javax.swing.JFrame;
import javax.swing.JDialog;
import javax.swing.JButton;
import javax.swing.JOptionPane;

import org.potomak.patientmanager.beans.model.CheckUpBean;
import org.potomak.patientmanager.beans.model.PatientBean;
import org.potomak.patientmanager.persistence.JPoxJDO;

/**
 *
 * @author  mobile
 */
public class NewCheckUpDialog extends JDialog {
    
    /** Creates new form NewCheckUpDialog */
    public NewCheckUpDialog(JFrame parent) {
        super(parent);
        
        setResizable(false);
        
        parent.setEnabled(false);
        parent.setFocusableWindowState(false);
        
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });
        
        initComponents();
        
        getRootPane().setDefaultButton(addCheckUpButton);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        patientInfoTab = new javax.swing.JPanel();
        jPanel1 = new javax.swing.JPanel();
        dateLabel = new javax.swing.JLabel();
        weightLabel = new javax.swing.JLabel();
        weightField = new javax.swing.JTextField();
        dateField = new javax.swing.JTextField();
        addCheckUpButton = new javax.swing.JButton();
        cancelCheckUpButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("New check up");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("CheckUp info"));

        dateLabel.setText("Date");

        weightLabel.setText("Weight (Kg)");

        dateField.setText((new SimpleDateFormat("dd/MM/yyyy")).format(new Date()));

        org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(weightLabel)
                    .add(dateLabel))
                .add(99, 99, 99)
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(dateField, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 242, Short.MAX_VALUE)
                    .add(weightField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 242, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(weightLabel)
                    .add(weightField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(18, 18, 18)
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(dateField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(dateLabel))
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        addCheckUpButton.setText("Add");
        addCheckUpButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addCheckUpButtonActionPerformed(evt);
            }
        });

        cancelCheckUpButton.setText("Cancel");
        cancelCheckUpButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelCheckUpButtonActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout patientInfoTabLayout = new org.jdesktop.layout.GroupLayout(patientInfoTab);
        patientInfoTab.setLayout(patientInfoTabLayout);
        patientInfoTabLayout.setHorizontalGroup(
            patientInfoTabLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(patientInfoTabLayout.createSequentialGroup()
                .addContainerGap()
                .add(patientInfoTabLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, patientInfoTabLayout.createSequentialGroup()
                        .add(cancelCheckUpButton)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(addCheckUpButton)))
                .addContainerGap())
        );
        patientInfoTabLayout.setVerticalGroup(
            patientInfoTabLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(patientInfoTabLayout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(18, 18, 18)
                .add(patientInfoTabLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(addCheckUpButton)
                    .add(cancelCheckUpButton))
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(patientInfoTab, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(patientInfoTab, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void formWindowClosing(java.awt.event.WindowEvent evt) {
    this.getParent().setEnabled(true);
    ((JFrame)this.getParent()).setFocusableWindowState(true);
    setVisible(false);
}
    
private void addCheckUpButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addCheckUpButtonActionPerformed
    JPoxJDO jpoxJDO = ((Contacts)this.getParent()).getJpoxJDO();
    
    boolean success = false;

        Transaction txn = jpoxJDO.getPersistenceManager().currentTransaction();
        try {
            txn.begin();

            ((Contacts)this.getParent()).getSelectedPatient().getCheckUps().add(
                    new CheckUpBean(
                        ((Contacts)this.getParent()).getSelectedPatient(),
                        new Double(weightField.getText().trim()),
                        (new SimpleDateFormat("dd/MM/yyyy")).parse(dateField.getText())
                    )
            );

            txn.commit();
            success = true;
        }
        catch(Exception e) {
            e.printStackTrace();
        }
        finally {
            if (txn.isActive()) {
                txn.rollback();
                success = false;
            }
        }
    
        
    if (!success) {
        JOptionPane.showMessageDialog(this, "Check up info not saved.","Error",JOptionPane.ERROR_MESSAGE);
    }
    
    ((Contacts)this.getParent()).updateCheckUpsTableData();
    this.getParent().setEnabled(true);
    ((JFrame)this.getParent()).setFocusableWindowState(true);
    setVisible(false);
}//GEN-LAST:event_addCheckUpButtonActionPerformed

private void cancelCheckUpButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelCheckUpButtonActionPerformed
    this.getParent().setEnabled(true);
    ((JFrame)this.getParent()).setFocusableWindowState(true);
    setVisible(false);
}//GEN-LAST:event_cancelCheckUpButtonActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton addCheckUpButton;
    private javax.swing.JButton cancelCheckUpButton;
    private javax.swing.JTextField dateField;
    private javax.swing.JLabel dateLabel;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel patientInfoTab;
    private javax.swing.JTextField weightField;
    private javax.swing.JLabel weightLabel;
    // End of variables declaration//GEN-END:variables

}
